//package com.example.springbootdemo.aop;
//
//import com.example.springbootdemo.annotation.RedisCache;
//import com.example.springbootdemo.util.CommonUtil;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Method;
//import java.util.Arrays;
//
///**
// * 利用redis直接返回该方法的返回值
// */
//
//@Aspect
//@Component
//public class RedisCacheAspect {
//
//    @Autowired
//    private RedisUtil redisUtil;
//
//    @Pointcut("@annotation(com.hzyl.admin.annotation.RedisCache)")
//    public void annotationPointCut() {
//    }
//
//    @Before("annotationPointCut()")
//    public void before(JoinPoint joinPoint) {
//    }
//
//    @Around("annotationPointCut()")
//    public Object advice(ProceedingJoinPoint joinPoint) throws Throwable {
//        MethodSignature sign = (MethodSignature) joinPoint.getSignature();
//        Method method = sign.getMethod();
//        if (VOID.equals(method.getReturnType().toString())) {
//            throw new RuntimeException("m-custom Method return type cannot be void");
//        }
//
//        RedisCache annotation = method.getAnnotation(RedisCache.class);
//        if (CommonUtil.isEmpty(annotation.key())) {
//            throw new RuntimeException("m-custom Reids key can not be empty");
//        }
//        Object[] args = joinPoint.getArgs();
//        String thisKey;
//        if (args.length == 0) {
//            thisKey = (A_S_REDIS_CACHE + annotation.key());
//        } else {
//            thisKey = (A_S_REDIS_CACHE + annotation.key() + SYMBOL_COLON + Arrays.toString(args));
//        }
//        long thisTimeOut = annotation.timeOut();
//        //如果redis里存在该返回值，则直接返回该返回值
//        Object redisReturnValue = redisUtil.get(thisKey);
//        if (CommonUtil.isNotEmpty(redisReturnValue)) {
//            return redisReturnValue;
//        } else {
//            //如果redis里不存在该返回值，则执行该方法获取其返回值，并将返回值存入redis
//            Object retmsg;
//            retmsg = joinPoint.proceed();
//            redisUtil.set(thisKey, retmsg, thisTimeOut);
//            return retmsg;
//        }
//    }
//
//    @After("annotationPointCut()")
//    public void after() {
//    }
//
//    @AfterReturning(value = "annotationPointCut()", returning = "retmsg")
//    public void afterReturning(JoinPoint joinPoint, Object retmsg) {
//    }
//
//    @AfterThrowing(value = "annotationPointCut()", throwing = "ex")
//    public void afterThrowing(JoinPoint joinPoint, Exception ex) {
//    }
//}
